<script setup lang="ts" name="NoticeEdit">
import { NOTICE_STATUS_ENUM } from "@/views/system/notice/const";
import { addNoticeApi, getNoticeApi, updateNoticeApi } from "@/api/system/notice";

const { proxy } = getCurrentInstance();

const { sys_notice_type, sys_notice_status } = useDict("sys_notice_type", "sys_notice_status");

const emit = defineEmits(['closeDialog']);

const props = defineProps({
  id: {
    default: null,
  },
  title: {
    type: String,
    default: false,
  }
});

// 通知公告表单
const noticeFormRef = ref(null);

// 弹窗开关
const open = ref(false);

// 表单数据
const form = ref<NoticeRecord>({});

// 表单校验
const rules = ref({
  noticeTitle: [
    { required: true, message: "公告标题不能为空", trigger: "blur" },
  ],
  noticeType: [
    { required: true, message: "公告类型不能为空", trigger: "change" },
  ],
  status: [
    { required: true, message: "状态不能为空", trigger: "change" },
  ],
  noticeContent: [
    { required: true, message: "内容不能为空", trigger: "blur" },
  ],
});

// 加载
const loading = ref(false);

/** 获取通知公告详情 */
function getData() {
  reset();
  if (isEmpty(props.id)) {
    open.value = true;
  } else {
    getNoticeApi(props.id).then(response => {
      form.value = response.data;
      open.value = true;
    }).catch(() => {
      closeDialog(false);
    })
  }
}

/** 表单重置 */
function reset() {
  form.value = {
    noticeId: null,
    noticeTitle: null,
    noticeType: null,
    status: NOTICE_STATUS_ENUM.normal,
    noticeContent: '',
    createBy: null,
    createTime: null,
  };
}

/** 关闭弹窗 */
function closeDialog(refresh: boolean) {
  open.value = false;
  emit("closeDialog", refresh);
}

/** 提交按钮 */
function submitForm() {
  // @ts-ignore
  noticeFormRef.value.validate((valid: boolean) => {
    if (valid) {
      loading.value = true;
      if (isEmpty(form.value.noticeId)) {
        addNoticeApi(form.value).then(() => {
          // @ts-ignore
          proxy.$modal.msgSuccess("新增成功");
          loading.value = false;
          closeDialog(true);
        }).catch(() => {
          loading.value = false;
        });
      } else {
        updateNoticeApi(form.value).then(() => {
          // @ts-ignore
          proxy.$modal.msgSuccess("修改成功");
          loading.value = false;
          closeDialog(true);
        }).catch(() => {
          loading.value = false;
        });
      }
    }
  })
}

watch(open, (val) => {
  if (!val) {
    closeDialog(false);
  }
});

getData();
</script>
<template>
  <!-- 通知公告编辑对话框 -->
  <el-dialog :title="title" v-model="open" width="700px" append-to-body :close-on-click-modal="false">
    <el-form ref="noticeFormRef" :model="form" :rules="rules" :inline="true" label-width="80px">
      <el-form-item label="公告标题" prop="noticeTitle" class="item-describe">
        <el-input type="textarea" v-model="form.noticeTitle" placeholder="请输入公告标题" />
      </el-form-item>
      <el-form-item label="公告类型" prop="noticeType">
        <el-select v-model="form.noticeType" placeholder="请选择公告类型">
          <el-option
              :label="item.label"
              :value="item.value"
              v-for="(item, index) in sys_notice_type"
              :key="index"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-radio-group v-model="form.status" placeholder="请选择状态">
          <el-radio
              :value="item.value"
              v-for="(item, index) in sys_notice_status"
              :key="index"
          >
            {{ item.label }}
          </el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="内容" prop="noticeContent" class="item-describe">
        <editor v-model="form.noticeContent" :min-height="192" />
      </el-form-item>
    </el-form>
    <template #footer>
      <div class="dialog-footer">
        <el-button type="primary" v-debounce="submitForm" :loading="loading">确 定</el-button>
        <el-button @click="closeDialog(false)">取 消</el-button>
      </div>
    </template>
  </el-dialog>
</template>
<style lang="scss" scoped>
:deep(.ql-editor) {
  overflow-y: auto;
  max-height: 350px;
}
</style>